package org.javabasics.io;

import java.nio.CharBuffer;

/**
 * Created by LUOLIANG on 2016/9/26.
 * java NIO 核心类  Buffer测试
 */
public class BufferTest {
    public static void main(String[] args) {
        //创建Buffer
        CharBuffer charBuffer = CharBuffer.allocate(8);
        System.out.println("capacity: " + charBuffer.capacity());
        System.out.println("limit: " + charBuffer.limit());
        System.out.println("position: " + charBuffer.position());
        //放入元素
        charBuffer.put('a');
        charBuffer.put('b');
        charBuffer.put('c');
        System.out.println("加入三个元素后，position：" + charBuffer.position());
        //调用flip方法   准备读取
        charBuffer.flip();
        System.out.println("执行flip后，limit=" + charBuffer.limit());
        System.out.println("position=" + charBuffer.position());
        //取出第一个数据后
        System.out.println("第一个元素（position=0）：" + charBuffer.get());
        System.out.println("取出第一个元素后，position=" + charBuffer.position());
        //调用clear方法
        charBuffer.clear();
        System.out.println("执行clear后，limit=" + charBuffer.limit());
        System.out.println("执行clear后，position=" + charBuffer.position());
        System.out.println("执行clear后，缓冲区内容并没有被清除，第三个元素为：" + charBuffer.get(2));
        System.out.println("执行绝对读取后，position=" + charBuffer.position());
    }
}
